package com.hontech.bv.ui

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.hontech.bv.R
import com.hontech.bv.ui.fragment.*


class Fragments {

    val items = arrayOf(
        SettingFragment(),
        PipeLineFragment(),
        PickFragment(),
        ArmFragment(),
        IceFragment(),
        PearlFragment(),
        PowderFragment(),
        HeatFragment()
    )
}

class ItemInfo(
    val text: String,
    val iconId: Int
)

class FgRouter(activity: AppCompatActivity) : FragmentStateAdapter(activity) {

    companion object {

        private const val TEXT_SETTING = "设置"
        private const val TEXT_PIPE_LINE = "流水线"
        private const val TEXT_PICK = "取物门"
        private const val TEXT_ARM = "机械臂"
        private const val TEXT_ICE = "制冰"
        private const val TEXT_PEARL = "珍珠架"
        private const val TEXT_POWDER = "粉料架"
        private const val TEXT_HEAT = "加热"

        private val tabItems = arrayOf(
            ItemInfo(TEXT_SETTING, R.drawable.ic_setting_n),
            ItemInfo(TEXT_PIPE_LINE, R.drawable.ic_pipe_line_n),
            ItemInfo(TEXT_PICK, R.drawable.ic_pick_n),
            ItemInfo(TEXT_ARM, R.drawable.ic_arm_n),
            ItemInfo(TEXT_ICE, R.drawable.ic_ice_n),
            ItemInfo(TEXT_PEARL, R.drawable.ic_pearl_n),
            ItemInfo(TEXT_POWDER, R.drawable.ic_powder_n),
            ItemInfo(TEXT_HEAT, R.drawable.ic_heat_n)
        )
    }

    private val vp = activity.findViewById<ViewPager2>(R.id.id_main_vp)
    private val tabs = activity.findViewById<TabLayout>(R.id.id_main_tablayout)
    private var fgs: Fragments? = null


    init {
        vp.adapter = this
        vp.offscreenPageLimit = tabItems.size
        TabLayoutMediator(tabs, vp) { tab, position ->
            tab.text = tabItems[position].text
            tab.setIcon( tabItems[position].iconId )
        }.attach()
    }

    override fun getItemCount(): Int {
        if (fgs == null) {
            fgs = Fragments()
        }
        return fgs!!.items.size
    }

    override fun createFragment(position: Int): Fragment {
        return fgs!!.items[position]
    }


}












